<template>
	<view class="assign">
		<zhilin-picker v-model="show" :data="selectData" @change="onChange" @confirm="confirmSeletc" />
	</view>
	
</template>

<script>
import zhilinPicker from '../zhilin-picker/zhilin-picker.vue';
import service from '../../common/service/new_file.js';
import apiData from '../../common/utils/apiData.js';
export default {
	name: 'assignCharacter',
	components: { zhilinPicker},
	data() {
		return {
			show: false,
			title: '选择',
			selectData: [], //渲染选择器的数据
			assignUser: [], //获取的原始数据
			workGroupeOptions: [],
			searchOption: []
		};
	},
	props: {
		action: '',
		billtype: '',
		formPk: '',//传入的表单id
		width: {
			default: '136'
		},
		height: {
			default: '46'
		}
	},
	watch: {},
	methods: {
		open() {
			this.show = true;
		},
		init() {
			this.getUserList();
		},
		concelEvent() {
			this.$emit('outConfirm',{pk:this.formPk,spr:[]});
		},
		confirmSeletc(userNames) {
			//选择选中人的pk，
			if (userNames.length == 0) {
				//如果不存选中的人直接传出事件
				this.concelEvent()
			} else {
				let sprs = userNames.map(item => {
					return (item = {
						spr: this.assignUser.filter(el => item == el.name)[0].value
					});
				});
				this.$emit('outConfirm', {pk:this.formPk,spr:sprs});
			}
			//标记指定人
		},
		getUserList() {
			//获取查询用户列表
			let billtype = this.billtype;
			let form_pk = this.formPk;
			let data = apiData.assign(form_pk, [], billtype, this.action,'N');
			service.common(data, {
				success: res => {
					if (JSON.stringify(res.data.VALUE) != '{}') {
						//获取可指派的用户并重新组合数据
						let newObj = JSON.parse(JSON.stringify(res.data.VALUE));
						let resArr = [];
						for (let key in newObj) {
							resArr.push({
								name: newObj[key],
								value: key
							});
						}
						this.assignUser = resArr;
						this.selectData = resArr.map(item => (item = item.name));
						
						uni.showModal({
							title: '提示',
							content: '检测到有可指派用户，是否进行指派？',
							success: mol => {
								if (mol.confirm) {
									//若确认指派则打开选择器
									this.open();
								} else if (mol.cancel) {
									//不指派的直接传出空数据列表
									this.concelEvent();
								}
							}
						});
					} else {
						this.concelEvent();
					}
				}
			});
		}
	},
	created() {}
};
</script>

<style lang="scss" scoped>
.user-wrap {
	width: 60vw;
	padding: 40rpx;
	border-radius: 16rpx;
	background: #7da4ff;
	overflow: hidden;
}
</style>
